home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0102_Config File.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-24  |  3KB  |  110 lines

  1. {
  2.  >Can anyone give me an idea of how to use a config file in my programs.
  3.  >Such as an easy one, I am writing a program for my BBS in which this
  4.  >program will Copy files to another directory.  I know I could put the
  5.  >directory from and to in the code itself, but what I want to accomplish
  6.  >is to use a Configuration file to read the from directory and to
  7.  >directory.  This is so the program can be used anywhere. Can someone
  8.  >please help me with this?
  9.  
  10. I posted a unit I wrote a day or so ago which can be modified to do this.
  11. Here it is again (extensively modified to support an ASCII configuration
  12. file):
  13.  
  14. Notes: Change the CFGKEYS constants to the keywords you want your program
  15. to recognize (remember to change the CONFIGOPTIONS constant also).
  16.  
  17. }
  18.  
  19. Unit CFG_DEF;
  20.  
  21. Interface uses Dos;  { Dos unit is needed for FindFirst }
  22.  
  23. Const
  24.  
  25. CONFIGFILE = 'YOURFILE.CFG';
  26. CONFIGOPTIONS = 5;
  27. CFGKEYS : array[1..CONFIGOPTIONS] of string = ('YOUR',
  28.                                                'CONFIG',
  29.                                                'OPTIONS',
  30.                                                'GO',
  31.                                                'HERE');
  32.  
  33. Procedure Read_Cfg_File;
  34.  
  35. Implementation {----------------------------------------------}
  36.  
  37. Function Findfile(searchkey : string) : boolean;
  38.  var srec : searchrec;
  39. begin
  40.  findfirst(searchkey,anyfile, srec);
  41.  FindFile := (doserror = 0);
  42. end;
  43.  
  44. Function Uppercase(st : string) : string;
  45.  var loop : byte;
  46. begin
  47.  for loop := 1 to length(st) do st[loop] := upcase(st[loop]);
  48.  uppercase := st;
  49. end;
  50.  
  51. Procedure Read_Cfg_File;
  52.  var f :text; i, j, loop : byte; line, key, command : string;
  53.      Result_Table : array[1..CONFIGOPTIONS] of boolean;
  54. begin
  55. fillchar(Result_Table,sizeof(Result_Table),false);
  56. command := #0;
  57. line := #0;
  58. key := #0;
  59.  
  60. {$I-}
  61. assign(f,CONFIGFILE);
  62. reset(f);
  63. {$I+}
  64. {CheckError(IOResult,CFGFILE);  <--- Add your own error checking here as
  65.                                      my CheckError procedure is not included
  66.                                      in this snippet. }
  67.  while not EOF(f) do begin {while}
  68.  readln(f,line);
  69.  
  70.  if (copy(line,1,1) <> #59) and
  71.     (copy(line,1,1) <> #32) then begin  { ignore lines preceeded with a
  72.                                          comment delimiter - usually #59
  73.                                          (IE: ';')}
  74.    j := pos(#32,line);
  75.  
  76.    if j = 0 then j := length(line)+1;
  77.      key := copy(line,1,j-1);
  78.      delete(line,1,j);
  79.    i := pos(#59,line);
  80.  
  81.    if i = 0 then i := length(line)+1;
  82.  
  83.    command := copy(line,1,i-1);
  84.    i := pos(#32,command);
  85.    if i <> 0 then delete(command,i,length(command)-(i-1));
  86.  
  87.      for loop := 1 to CONFIGOPTIONS do begin {loop}
  88.        if Uppercase(key) = CFGKEYS[loop] then begin {if}
  89.          Result_Table[loop] := true;
  90.          case loop of {case}
  91.             1 : begin
  92.                 end;
  93.             2 : begin
  94.                 end;
  95.             3 : begin
  96.                 end;
  97.             4 : begin
  98.                 end;
  99.             5 : begin
  100.                 end;
  101.              end; {case}
  102.           end; {if}
  103.        end; {loop}
  104.     end; {if}
  105.  end; {while}
  106. close(f);
  107. end; {proc}
  108.  
  109. end. {unit}
  110.